<template>
  <div />
</template>

<script>
import Qs from 'qs'
export default {
  data() {
    return {
      ts: this.newStore(),
      username: ''
    }
  },
  mounted() {
    
    const query = Qs.parse(window.location.search.substring(1))
    const code = query.code
    // if (code && code != '') {
    //   // 从微信转回，进行服务器登录
    //   this.doLogin(code)
    //   return
    // }
    if (config.MODE == 'dev') {
      this.userLogin2()
      return
    } else {
      if (code && code != '') {
        // 从微信转回，进行服务器登录
        this.doLogin(code)
        return
      }
    }
    // }

    // // 判断登录
    this.getWxCode()
  },
  methods: {
    getWxCode() {
      const wxLoginUrl =
        'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf30a6608e950a663&redirect_uri=http://vipcard.eke88.com&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'

      // 改成ajax试试
      window.location = wxLoginUrl
    },
    doLogin(code) {
      const res = {
        code: code
      }
      this.spost(this.ts, '/Register/getWeixinCode', res).then(ts => {
        // if (ts.d.flag == '200') {
        console.log(ts.d.data)
        localStorage.setItem('token', ts.d.data.token)
        // console.log(localStorage.getItem('token'))
        // localStorage.setItem('identity', ts.d.data.identity)
        // this.$router.push({ name: 'ephome' })
        // }
        this.carList()
        // this.$router.push({ name: 'fhome' })
      })
      console.log('doLogin', code)
    },
    carList() {
      const res = {
        token: localStorage.getItem('token')
      }
      this.spost(this.ts, '/Carc/carList').then(ts => {
        if (ts.d.flag == '200') {
          console.log(ts.d.data.length,'22344');
          this.$root.env.cardlength = ts.d.data.length
          this.$router.push({ name: 'fhome' })
        }
      })
    },
    userLogin2() {
      const res = {
        openid: 'lwy1'
      }
      this.spost(this.ts, '/Register/mn', res).then(ts => {
        if (ts.d.flag == '200') {
          console.log(ts.d.data)
          localStorage.setItem('token', ts.d.data.token)
          console.log(localStorage.getItem('token'))
          localStorage.setItem('identity', ts.d.data.identity)
          console.log(localStorage.getItem('identity'))
          this.carList()
        }
      })
    }
  }
}
</script>

<style>
</style>
